﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Management;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using Sunny.UI.Win32;
using System.Globalization;


namespace ALL_Tool
{
    public partial class EncryptionExport : UIPage
    {
        public EncryptionExport()
        {
            InitializeComponent();
        }

        //初始化年月日下拉菜单
        private void EncryptionExport_Load(object sender, EventArgs e)
        {
            txtYear.Items.Clear();
            int startYear = 2024;
            int numberOfYears = 100;

            // 使用for循环从2024年开始添加100个年份
            for (int i = 0; i < numberOfYears; i++)
            {
                txtYear.Items.Add((startYear + i).ToString());
            }
            // 设置默认选中项为第一个年份
            txtYear.SelectedIndex = 0;
            // 设置默认选中项为第一个年份
            txtMonth.SelectedIndex = 0;



            // 添加月份
            txtMonth.Items.Clear();
            for (int month = 1; month <= 12; month++)
            {
                txtMonth.Items.Add(month.ToString("D2"));
            }
            // 设置默认选中项为第一个月份
            txtMonth.SelectedIndex = 0;

            //初始化日的下拉框选项
            txtDay.Items.Clear();
            // 添加选项
            txtDay.Items.Add("01");
            txtDay.Items.Add("15");
            txtDay.SelectedIndex = 0;
        }


        /// 不懂这一串是什么意思,以后研究
        [DllImport("Kernel32.dll")]
        private static extern bool SetSystemTime(ref SYSTEMTIME st);

        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public short Year;
            public short Month;
            public short DayOfWeek;
            public short Day;
            public short Hour;
            public short Minute;
            public short Second;
            public short Milliseconds;
        }

        //设置时间  并实现自动增加时间
        private void setTime_Click(object sender, EventArgs e)
        {
            // 从文本框中获取年、月、日
            string year = txtYear.Text;
            string month = txtMonth.Text;
            string day = txtDay.Text;

            // 组合日期字符串
            string newDateStr = $"{year}-{month}-{day}";
            
            //修改日期
            if (DateTime.TryParseExact(newDateStr, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime newDate))
            {
                // 转换DateTime为SYSTEMTIME
                SYSTEMTIME st = new SYSTEMTIME
                {
                    Year = (short)newDate.Year,
                    Month = (short)newDate.Month,
                    Day = (short)newDate.Day,
                    Hour = 0, // 假设设置时间为午夜
                    Minute = 0,
                    Second = 0,
                    Milliseconds = 0
                };

                // 设置系统时间
                if (SetSystemTime(ref st))
                {
                    MessageBox.Show("系统日期已更新。");
                }
                else
                {
                    MessageBox.Show("更新系统日期失败。请以管理员身份运行程序。");
                }
            }
            else
            {
                MessageBox.Show("请输入有效的日期格式（yyyy-MM-dd）！");
            }
            //设置月份 如果不等于  12  则 +2

            //设置 日  如果等于 15  让 日等于01  且年份加1并判断月份是不是12  是的话  月份等于02
                if (day == "15")
                {
                    // 将日期设置为01号
                    txtDay.SelectedIndex = txtDay.Items.IndexOf("01");

                    // 判断当前月份是否为12
                    if (month == "12")
                    {
                        // 如果是12月，则年份加1，并将月份设置为02
                        int newYear = int.Parse(year) + 1;
                        // 确保年份项在控件中
                        if (!txtYear.Items.Contains(newYear.ToString()))
                        {
                        txtYear.Items.Add(newYear.ToString());
                        }
                        // 确保月份项在控件中
                        if (!txtMonth.Items.Contains("02"))
                        {
                        txtMonth.Items.Add("02");
                        }

                            txtMonth.SelectedIndex = txtMonth.Items.IndexOf("02");
                            txtYear.SelectedIndex = txtYear.Items.IndexOf(newYear.ToString());
                    }
                    else
                    {
                        // 如果不是12月，则月份加2
                        int newMonth = int.Parse(month) + 2;
                        string newMonthStr = newMonth.ToString("00");

                        // 确保月份项在控件中
                        if (!txtMonth.Items.Contains(newMonthStr))
                        {
                        txtMonth.Items.Add(newMonthStr);
                        }

                    txtMonth.SelectedIndex = txtMonth.Items.IndexOf(newMonthStr);
                    }
                }
                else
                {
                    // 设置日期为15号
                    txtDay.SelectedIndex = txtDay.Items.IndexOf("15");
                }




        }

        private void EncryptionExport_Initialize(object sender, EventArgs e)
        {

        }

        private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        //执行加密字符插入语句
        private void insData_Click(object sender, EventArgs e)
        {
            string connectionString = PublicMethod.Sql_Link_Str();
            string query = "DECLARE @d1 VARCHAR(10);\r\nDECLARE @d2 VARCHAR(10);\r\nSET @d1 = CONVERT(VARCHAR(10), GETDATE(), 120);\r\nSET @d2 = CONVERT(VARCHAR(10), DATEADD(MONTH, -1, GETDATE()), 120);\r\nDECLARE @st1 TABLE (var_value VARCHAR(24))\r\nINSERT INTO @st1 (var_value)\r\nSELECT var_value\r\nFROM a1.dbo.t_sys_reg\r\nWHERE var_id = 'Sys_Rge_Use'\r\nDECLARE @st1Value VARCHAR(24)\r\nSELECT @st1Value = var_value FROM @st1\r\nDECLARE @st2 TABLE (var_value VARCHAR(24))\r\nINSERT INTO @st2 (var_value)\r\nSELECT var_value\r\nFROM a1.dbo.t_sys_reg\r\nWHERE var_id = 'Sys_Rge_Operation'\r\nDECLARE @st2Value VARCHAR(24)\r\nSELECT @st2Value = var_value FROM @st2\r\nINSERT INTO t_sys_date (d,st)\r\nVALUES (@d1,@st1Value), (@d2,@st2Value)\r\n";
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        SqlDataReader reader = command.ExecuteReader();
                        // Optionally handle data from the reader here
                        MessageBox.Show("执行完成！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Query Failed: {ex.Message}");
            }
        }
        //删除a1数据库
        private void delDatabase_Click(object sender, EventArgs e)
        {
            string connectionString = PublicMethod.Sql_Link_Str();
            string query = "DROP DATABASE a1";
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (SqlCommand sqlCommand = new SqlCommand(query, connection))
                    {
                        SqlDataReader reader = sqlCommand.ExecuteReader();
                        MessageBox.Show("删除成功");
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show($"Query Failed: {ex.Message}");
            
            }
        }
    }
}
